package com.df.ez_game.tools;

import com.df.ez_game.server.EzGameBusinessException;
import com.df.ez_game.server.IPushMsgEnum;
import com.df.ez_game.server.PushMsgMeta;
import com.df.ez_game.server.logic.status_sync.CodeGenerator;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.Set;

public class EzUtils {
    public static void exceptionDataHandle(Set<Class> exceptionClass, Map<Integer, String> exceptionMsgMap) {
        for (Class aClass : exceptionClass) {
            Field[] declaredFields = aClass.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                int modifiers = declaredField.getModifiers();
                if (!Modifier.isStatic(modifiers) ) {
                    continue;
                }
                if (!Modifier.isFinal(modifiers)) {
                    continue;
                }
                if(!EzGameBusinessException.class.isAssignableFrom(declaredField.getType())){
                    continue;
                }
                try {
                    EzGameBusinessException f = (EzGameBusinessException) declaredField.get(null);
                    Integer code = f.getCode();
                    String message = f.getMessage();
                    if(exceptionMsgMap.containsKey(code)){
                        System.out.println("有重复的异常码："+code);
                    }
                    exceptionMsgMap.put(code,message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void pushEnumsHandle(Set<Class> pushEnums,Map<Integer,PushMsgMeta> pushMsgMap) {
        for (Class pushEnum : pushEnums) {
            if(!pushEnum.isEnum()){
                throw new RuntimeException("推送消息的映射类必须为枚举："+pushEnum.getSimpleName());
            }
            if(!IPushMsgEnum.class.isAssignableFrom(pushEnum)){
                throw new RuntimeException("推送消息的映射类必须实现接口："+IPushMsgEnum.class.getSimpleName());
            }
            Object[] enumConstants = pushEnum.getEnumConstants();
            for (Object enumConstant : enumConstants) {
                IPushMsgEnum pushMsgEnum = (IPushMsgEnum) enumConstant;
                PushMsgMeta exitMeta = pushMsgMap.get(pushMsgEnum.getCmd());
                Enum em = (Enum) enumConstant;
                String pushMsgName = em.name();
                if(exitMeta!=null){
                    throw new RuntimeException("推送消息的映射重复：["+exitMeta.getEnumClass().getSimpleName()
                            +"--"+exitMeta.getPushName()+"--"+exitMeta.getCmd()+"] 和 ["+
                            pushEnum.getSimpleName()+"--"+pushMsgName+"--"+pushMsgEnum.getCmd()+"]"
                    );
                }
                PushMsgMeta msgMeta = new PushMsgMeta();
                msgMeta.setCmd(pushMsgEnum.getCmd());
                msgMeta.setPushMsgClass(pushMsgEnum.getClazz());
                msgMeta.setEnumClass(pushEnum);
                msgMeta.setPushName(pushMsgName);
                pushMsgMap.put(pushMsgEnum.getCmd(),msgMeta);
            }
        }
    }

    public static void protoEntityCollect(Set<Class> classes, Set<CodeGenerator.ProtoEntity> protoEntity) {
        if(classes==null){
            return;
        }
        CodeGenerator.ProtoEntity entity = new CodeGenerator.ProtoEntity();
        for (Class aClass : classes) {
            entity.setEntityName(aClass.getSimpleName());
            Field[] declaredFields = aClass.getDeclaredFields();
        }
    }
}
